package com.tutu.sys.controller;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tutu.common.util.DatatablesJson;
import com.tutu.common.util.JsonReturnObject;
import com.tutu.sys.entity.SysLog;
import com.tutu.sys.service.ISysLogService;

@Controller
@RequestMapping("/system")
public class SystemAction {
	
	protected Logger logger = LoggerFactory.getLogger(getClass());
	@Resource
	private ISysLogService sysLogService;

	@RequestMapping(value="/querySysLog")
	public String querySysLog() throws IOException{
		return "system/querySysLog";
	}
	
	@RequestMapping("/getSysLogs")
	@ResponseBody
	public JsonReturnObject<DatatablesJson<SysLog>> getSysLogs(@RequestParam String sEcho, @RequestParam int iDisplayStart,@RequestParam int iDisplayLength){
		JsonReturnObject<DatatablesJson<SysLog>> jsonReturnObject = new JsonReturnObject<DatatablesJson<SysLog>>(false,"获取日志失败");
		PageHelper.startPage((iDisplayStart/iDisplayLength)+1, iDisplayLength);
		try {
			List<SysLog> sysLogs = this.sysLogService.getSysLogs(null);
			DatatablesJson<SysLog> datatablesJson = new DatatablesJson<SysLog>();
			datatablesJson.setEcho(sEcho);
			PageInfo<SysLog> pageInfo = new PageInfo<SysLog>(sysLogs);
			datatablesJson.setTotalRecords(Long.valueOf(pageInfo.getTotal()).intValue());
			datatablesJson.setTotalDisplayRecords(Long.valueOf(pageInfo.getTotal()).intValue());
			datatablesJson.setData(pageInfo.getList());
			jsonReturnObject.setIsSuccess(true);
			jsonReturnObject.setReturnObject(datatablesJson);
		} catch (Exception e) {
			logger.error("获取日志", e);
		}
		return jsonReturnObject;
	}
}
